<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="js/jquery.min.js?v=2.1.4"></script>
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
		<script src="js/doT.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
        <style type="text/css">
            /*.layui-table-cell{*/
            /*    height: 100%;*/
            /*    max-width: 100%;*/
            /*}*/
            .layui-table-cell{
                /*height:80px; */ /*设置此参数，表头高度也改变*/
                height: auto; /*设置此样式，可以正常显示图片，显示多行文字。但不能固定列，否则表格样式乱套*/
            }
            .namedesc{
                white-space: normal;/*设置此样式，可以显示完整的文字*/
            }
        </style>
    </head>
    
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">旅游列表</a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input" autocomplete="off" placeholder="开始日" name="start" id="start"></div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input" autocomplete="off" placeholder="截止日" name="end" id="end"></div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" name="name" id="name" placeholder="请输入名称" autocomplete="off" class="layui-input"></div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn" id="search">
                                        <i class="layui-icon">&#xe615;</i></button>
                                </div>

                            </form>
                            
                            
                        </div>

                        <div class="layui-card-header">
                            <button class="layui-btn" onclick="xadmin.open('添加','./addGoods.html',900,700)"><i class="layui-icon"></i>添加</button>
                            <button class="layui-btn layui-btn-danger" id="batchDel"><i class="layui-icon"></i>批量删除</button>
                        </div>


                        <div class="layui-card-body ">

                            <table class="layui-hide" id="main_table" lay-filter="main_table" lay-data="{height:500}"></table>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>

    <script type="text/html" id="operation_bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit"  ><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" ><i class="layui-icon layui-icon-delete"></i>删除</a>
    </script>
<script>

    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#main_table'
            ,url:'/mgr/goods/list'
            ,method:'post'
            ,where: {"status":1,}
            ,cols: [[
                {type:'checkbox'}
                ,{field:'id', width:80, title: 'ID',hide:true}
                ,{field:'img',align:'center', width:220, title: '缩略图'
                    ,templet: '<div><img src="../{{d.img}}" width="200px"/></div>'}
                ,{ width:500, title: '名称',
                    templet: '<div><div class="namedesc">{{d.name}} · {{d.descrip}}</div></div>'}
                ,{field:'price', width:80, title: '价格'}
                ,{field:'personNum',width:80, title: '旅游人数'}
                ,{align:'center', title: '操作', toolbar: '#operation_bar'}
            ]]
            ,page: true
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            ,id:'main_table'
        });


        //监听工具条
        table.on('tool(main_table)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;

            if(layEvent === 'edit'){
                xadmin.open('修改','./showGoods.html?id='+data.id,900,700)
            }else if(layEvent === 'del'){
                del(data.id,obj)
            }
        });


        $('#search').on('click', function(){
            var name = $('#name');
            var start = $('#start');
            var end = $('#end');

            //执行重载
            table.reload('main_table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    name: name.val(),
                    startDate: start.val(),
                    endDate: end.val(),
                    status:1,
                }
            }, 'data');

        });

        $('#batchDel').on('click', function(){
            var checkStatus = table.checkStatus('main_table')
            var datas = checkStatus.data;

            dels(datas)

        });

    });
    layui.use(['laydate'], function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });


    });

function del(id,obj){
	layer.confirm('您确定要删除选中的数据吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			$.ajax({   
		        url:'goods/deleteByIds',   
		        type:'post',   
		        dataType:'json',//服务器端返回的数据格式是json
				   data: {
					   "ids":[id]
				   },//发给服务器端的数据
		        success:function(res){   //data:服务器端返回给浏览器端的数据
				   		if(res.code==0){
				   			//alert(res.msg)
                            obj.del();
				   			layer.msg('删除成功！');
				   		}else{
				   			alert(res.msg);
				   		}
		        },
				   error:function (XMLHttpRequest, textStatus, errorThrown) {
		     	   		alert(XMLHttpRequest);
		     	   		alert(textStatus);
		     	   		alert(errorThrown);
		        }
		        
		    });
		}, function(){
		  
		});
	
}
function dels(datas){
	
	var ids =[];

    var ids =[];
    for(var i=0;i<datas.length;i++){
        ids.push(datas[i].id);
    }

	if(ids.length==0){
		layer.msg('请勾选要删除的数据！');
		return;
	}

	layer.confirm('您确定要删除选中的数据吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			$.ajax({   
		        url:'goods/deleteByIds',   
		        type:'post',   
		        dataType:'json',//服务器端返回的数据格式是json
				   data: {
					   "ids":ids
				   },//发给服务器端的数据
		        success:function(res){   //data:服务器端返回给浏览器端的数据
				   		if(res.code==0){
				   			layer.msg('删除成功！');
                            location.reload()
				   		}else{
				   			alert(res.msg);
				   		}
		        },
				   error:function (XMLHttpRequest, textStatus, errorThrown) {
		     	   		alert(XMLHttpRequest);
		     	   		alert(textStatus);
		     	   		alert(errorThrown);
		        }
		        
		    });
		}, function(){
		  
		});
	
	
	
}
</script>

</html>